﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Sunyi.Project.WebPoint.DataContract;

namespace Sunyi.Project.WebPoint.DataAccessController
{
    public partial class PageDaoController
    {
        public Collection<Page> SelectAll()
        {
            Collection<Page> items = dao.SelectAll();

            CacheProvider.Instance.Clear<Page>();
            foreach (var item in items) CacheProvider.Instance.Add<Page>(item.Id, item);

            return items;
        }

        public Page SelectByUrl(string url)
        {
            Page page = null;
            Dictionary<Guid, object> pages = CacheProvider.Instance.Get<Page>();

            foreach (object obj in pages.Values)
            {
                Page item = obj as Page;

                if (item != null && item.Url == url)
                {
                    page = item;
                    break;
                }
            }

            if (page == null)
            {
                page = dao.SelectByUrl(url);

                if (page != null) CacheProvider.Instance.Add<Page>(page.Id, page);
            }

            return page;
        }
    }
}
